import {
  listAll as listAllInfo
} from '../services/hot';
import {
  statisticArticleCount as countArticleInfo,
  statisticBaiJiaCount as countBaiJiaInfo,
  statisticHotSearch as countSearchInfo,
  statisticHotTopic as countTopicInfo,
  statisticArticleGroup as groupArticleInfo,
  statisticBaiJiaGroup as groupBaiJiaInfo,
  statisticArticleRank as rankArticleInfo,
  statisticBaiJiaRank as rankBaiJiaInfo,
  statisticCategoryRatio as categoryRatioInfo,
} from '../services/statistics';

export default {
  namespace: 'statistics',

  state: {
    articleCountData: {
      total: 0,
      maxViewEntity: {
        view: 0,
        src: '',
      },
    },
    baiJiaCountData: {
      total: 0,
      maxFansEntity: {
        fans: 0,
        home_url: '',
      },
    },
    searchCountData: {
      total: 0,
      maxSearchEntity: {
        search_count: 0,
      },
      countList: [],
    },
    topicCountData: {
      total: 0,
      maxNewEntity: {
        title: '',
        title_src: '',
      },
    },
    articleGroupData: [],
    baiJiaGroupData: [],
    articleRankData: [],
    baiJiaRankData: [],
    hotData: [],
    ratioData: [],
    ratioTotal: 0,
  },

  effects: {
    *fetchCount(_, { call, put }){
      const articleResponse = yield call(countArticleInfo);
      const searchResponse = yield call(countSearchInfo);
      const topicResponse = yield call(countTopicInfo);
      const baiJiaResponse = yield call(countBaiJiaInfo);
      yield put({
        type: 'loadData',
        payload: {
          articleCountData: articleResponse.code === 0 ? articleResponse.data : this.state.articleCountData,
          searchCountData: searchResponse.code === 0 ? searchResponse.data : this.state.searchCountData,
          topicCountData: topicResponse.code === 0 ? topicResponse.data : this.state.topicCountData,
          baiJiaCountData: baiJiaResponse.code === 0 ? baiJiaResponse.data : this.state.baiJiaCountData,
        },
      });
    } ,

    *fetchGroup(_, { call, put }) {
      const articleResponse = yield call(groupArticleInfo);
      const baiJiaResponse = yield call(groupBaiJiaInfo);
      yield put({
        type: 'loadData',
        payload: {
          articleGroupData: articleResponse.code === 0 ? articleResponse.data : this.state.articleGroupData,
          baiJiaGroupData: baiJiaResponse.code === 0 ? baiJiaResponse.data : this.state.baiJiaGroupData,
        },
      });
    },

    *fetchRank({ payload }, { call, put }){
      const articleResponse = yield call(rankArticleInfo, payload);
      const baiJiaResponse = yield call(rankBaiJiaInfo, payload);
      yield put({
        type: 'loadData',
        payload: {
          articleRankData: articleResponse.code === 0 ? articleResponse.data : this.state.articleRankData,
          baiJiaRankData: baiJiaResponse.code === 0 ? baiJiaResponse.data : this.state.baiJiaRankData,
        },
      });
    } ,

    *fetchHot({ payload }, { call, put }){
      const response = yield call(listAllInfo, payload);
      response.data.forEach((item, index) => {
        item.rank = index + 1;
      });
      yield put({
        type: 'loadData',
        payload: {
          hotData: response.code === 0 ? response.data : this.state.hotData,
        },
      });
    },
    *fetchRatio(_, { call, put }){
      const response = yield call(categoryRatioInfo);
      yield put({
        type: 'loadData',
        payload: {
          ratioData: response.code === 0 ? response.data.list : this.state.ratioData,
          ratioTotal: response.code === 0 ? response.data.total : this.state.ratioTotal,
        },
      });
    },
  },

  reducers: {

    loadData(state, { payload }){
      return {
        ...state,
        ...payload,
      }
    },

    clear(state) {
      return {
        ...state,
      };
    },
  },
};
